package com.api.client.impl;

import org.scribe.model.Token;

import com.api.client.model.ApiLoginHandler;

public class ApiLoginHandlerImpl implements ApiLoginHandler {

	private String accessKey;
	private String accessSecret;

	private final OAuthProvider provider;

	public ApiLoginHandlerImpl(OAuthProvider provider) {

		if (provider == null) {

			throw new IllegalArgumentException("Provider cannot be null");
		}
		this.provider = provider;
	}

	public void setAccessKeys(String accessKey, String accessSecret) {

		this.accessKey = accessKey;
		this.accessSecret = accessSecret;
	}

	public void login(String username, String password) {

		Token accessToken = provider.getAccessToken(username, password);

		this.accessKey = accessToken.getToken();
		this.accessSecret = accessToken.getSecret();
	}

	public boolean isLoggedIn() {

		return accessKey != null && accessSecret != null;
	}

	public String getAccessKey() {
		return accessKey;
	}

	public String getAccessSecret() {
		return accessSecret;
	}
}
